4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
20 public class MathObject
: JSObject
{
21 public const double E
= 2.7182818284590452354;
22 public const double LN10
= 2.302585092994046;
23 public const double LN2
= 0.6931471805599453;
24 public const double LOG2E
= 1.4426950408889634;
25 public const double LOG10E
= 0.4342944819032518;
26 public const double PI
= 3.14159265358979323846;
27 public const double SQRT1_2
= 0.7071067811865476;
28 public const double SQRT2
= 1.4142135623730951;
30 private static readonly System
.Random internalRandom
= new System
.Random();
31 internal static MathObject ob
= null;
33 internal MathObject(ScriptObject parent
)
37 [JSFunctionAttribute(0, JSBuiltin
.Math_abs
)]
38 public static double abs(double d
){
49 [JSFunctionAttribute(0, JSBuiltin
.Math_acos
)]
50 public static double acos(double x
){
51 return System
.Math
.Acos(x
);
54 [JSFunctionAttribute(0, JSBuiltin
.Math_asin
)]
55 public static double asin(double x
){
56 return System
.Math
.Asin(x
);
59 [JSFunctionAttribute(0, JSBuiltin
.Math_atan
)]
60 public static double atan(double x
){
61 return System
.Math
.Atan(x
);
64 [JSFunctionAttribute(0, JSBuiltin
.Math_atan2
)]
65 public static double atan2(double dy
, double dx
){
66 return System
.Math
.Atan2(dy
, dx
);
69 [JSFunctionAttribute(0, JSBuiltin
.Math_ceil
)]
70 public static double ceil(double x
){
71 return System
.Math
.Ceiling(x
);
74 private static double Compare(double x
, double y
){
76 if (x
== y
) //x and y could be infinities, in which case - will not return 0.
80 double x1
= 1 / x
; //will be < 0 if x == -0
83 return y1
< 0 ? 0 : -1;
90 [JSFunctionAttribute(0, JSBuiltin
.Math_cos
)]
91 public static double cos(double x
){
92 return System
.Math
.Cos(x
);
95 [JSFunctionAttribute(0, JSBuiltin
.Math_exp
)]
96 public static double exp(double x
){
97 return System
.Math
.Exp(x
);
100 [JSFunctionAttribute(0, JSBuiltin
.Math_floor
)]
101 public static double floor(double x
){
102 return System
.Math
.Floor(x
);
105 internal override String
GetClassName(){
109 [JSFunctionAttribute(0, JSBuiltin
.Math_log
)]
110 public static double log(double x
){
111 return System
.Math
.Log(x
);
114 [JSFunctionAttribute(JSFunctionAttributeEnum
.HasVarArgs
, JSBuiltin
.Math_max
)]
115 public static double max(Object x
, Object y
, params Object
[] args
){
117 return Double
.NegativeInfinity
;
118 double dx
= Convert
.ToNumber(x
);
121 double dy
= Convert
.ToNumber(y
);
122 double result
= MathObject
.Compare(dx
, dy
);
123 if (result
!= result
) return result
;
125 if (result
< 0) lhMax
= dy
;
126 if (args
.Length
== 0) return lhMax
;
127 return MathObject
.maxv(lhMax
, args
, 0);
130 private static double maxv(double lhMax
, Object
[] args
, int start
){
131 if (args
.Length
== start
)
133 double head
= Convert
.ToNumber(args
[start
]);
134 double result
= MathObject
.Compare(lhMax
, head
);
135 if (result
!= result
) return result
;
136 if (result
> 0) head
= lhMax
;
137 return MathObject
.maxv(head
, args
, start
+1);
140 [JSFunctionAttribute(JSFunctionAttributeEnum
.HasVarArgs
, JSBuiltin
.Math_min
)]
141 public static double min(Object x
, Object y
, params Object
[] args
){
143 return Double
.PositiveInfinity
;
144 double dx
= Convert
.ToNumber(x
);
147 double dy
= Convert
.ToNumber(y
);
148 double result
= MathObject
.Compare(dx
, dy
);
149 if (result
!= result
) return result
;
151 if (result
> 0) lhMin
= dy
;
152 if (args
.Length
== 0) return lhMin
;
153 return MathObject
.minv(lhMin
, args
, 0);
156 private static double minv(double lhMin
, Object
[] args
, int start
){
157 if (args
.Length
== start
)
159 double head
= Convert
.ToNumber(args
[start
]);
160 double result
= MathObject
.Compare(lhMin
, head
);
161 if (result
!= result
) return result
;
162 if (result
< 0) head
= lhMin
;
163 return MathObject
.minv(head
, args
, start
+1);
166 [JSFunctionAttribute(0, JSBuiltin
.Math_pow
)]
167 public static double pow(double dx
, double dy
){
170 if ((dx
== 1.0 || dx
== -1.0) && (dy
== Double
.PositiveInfinity
|| dy
== Double
.NegativeInfinity
))
172 if (Double
.IsNaN(dy
))
175 // If dy is an odd integer, return -0. This case is inconsistent between x86 and amd64.
176 if (dx
== Double
.NegativeInfinity
&& dy
< 0.0) {
177 if (Math
.IEEERemainder((-dy
)+1.0, 2.0) == 0.0)
182 return System
.Math
.Pow(dx
, dy
);
184 if (dx
!= dx
|| dy
!= dy
)
188 if ((double)(long)dy
== dy
&& ((long)(-dy
)) % 2 > 0){
191 return Double
.NegativeInfinity
;
193 return Double
.PositiveInfinity
;
199 [JSFunctionAttribute(0, JSBuiltin
.Math_random
)]
200 public static double random(){
201 return MathObject
.internalRandom
.NextDouble();
204 [JSFunctionAttribute(0, JSBuiltin
.Math_round
)]
205 public static double round(double d
){
209 return System
.Math
.Floor(d
+0.5);
212 [JSFunctionAttribute(0, JSBuiltin
.Math_sin
)]
213 public static double sin(double x
){
214 return System
.Math
.Sin(x
);
217 [JSFunctionAttribute(0, JSBuiltin
.Math_sqrt
)]
218 public static double sqrt(double x
){
219 return System
.Math
.Sqrt(x
);
222 [JSFunctionAttribute(0, JSBuiltin
.Math_tan
)]
223 public static double tan(double x
){
224 return System
.Math
.Tan(x
);